//
//  LaunchController.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/25.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import UIKit
import Reusable

class LaunchController: BaseCollectionViewController {
    
    var list: [String] = [
        "ic_launch_01",
        "ic_launch_02",
        "ic_launch_03"
    ]
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        
        DispatchQueue.global().async {
            /// 异步线程
            let isNetworkConnect = UIDevice.isNetworkConnect
            xLog("网络 \(isNetworkConnect ? "已授权": "未授权")")
        }
    }
}

extension LaunchController {
    func initUI() {
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.register(cellType: LaunchItemCell.self)
    }
}

extension LaunchController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: LaunchItemCell.self)
        cell.iconImageView.image = list[indexPath.row].toImage
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        CGSize(width: xScreenW, height: xScreenH)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 2 {
            xLog("跳转页面")
            let token = UserProfile.token
            xLog("Token: \(token)")
            if token.isEmpty {
                // 未登录，默认中文
                // LanguageManager.setLanguage(.zh)
                xKeyWindow.rootViewController = BaseNavigationController(rootViewController: LoginMainController())
                
            } else {
                // 根据注册时选中的国家类型，同步本地化语言
                LanguageManager.setLanguage(UserProfile.country)
                xKeyWindow.rootViewController = BaseTabBarController()
            }
        }
    }
    
    
}
